package org.bkhech.mybatis.extend.generator.internal;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Properties;

/**
 * 重写注释
 *
 * Created by guowm on 2017/2/10.
 */
public class CustomCommentGenerator implements CommentGenerator {

    private final Properties properties = new Properties();
    private boolean suppressDate = false;
    private boolean suppressAllComments = false;

    public CustomCommentGenerator() {
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);
        this.suppressDate = StringUtility.isTrue(properties.getProperty("suppressDate"));
        this.suppressAllComments = StringUtility.isTrue(properties.getProperty("suppressAllComments"));
    }

    @Override
    public void addRootComment(XmlElement rootElement) {
        rootElement.addElement(new TextElement("<!--"));
        rootElement.addElement(new TextElement("    This Mapper File generated by MyBatis Generator Extend, Please do not modify it."));
        rootElement.addElement(new TextElement("-->"));
        rootElement.addElement(new TextElement(""));
    }

    @Override
    @SuppressWarnings("Duplicates")
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if(!this.suppressAllComments) {
            StringBuilder sb = new StringBuilder();
            field.addJavaDocLine("/**");
            if (introspectedColumn.getRemarks() != null) {
                field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
            }
            sb.append(" * ");
            sb.append(introspectedColumn.getActualColumnName());
            field.addJavaDocLine(sb.toString());
            field.addJavaDocLine(" */");
        }
    }

    @Override
    @SuppressWarnings("Duplicates")
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if(!this.suppressAllComments) {
            StringBuilder sb = new StringBuilder();
            method.addJavaDocLine("/**");
            if (introspectedColumn.getRemarks() != null) {
                method.addJavaDocLine(" * " + introspectedColumn.getRemarks());
            }
            sb.append(" * ");
            sb.append(introspectedColumn.getActualColumnName());
            method.addJavaDocLine(sb.toString());
            method.addJavaDocLine(" */");
        }
    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if(!this.suppressAllComments) {
            StringBuilder sb = new StringBuilder();
            method.addJavaDocLine("/**");
            if (introspectedColumn.getRemarks() != null) {
                method.addJavaDocLine(" * " + introspectedColumn.getRemarks());
            }
            sb.append(" * ");
            sb.append(introspectedColumn.getActualColumnName());
            method.addJavaDocLine(sb.toString());
            method.addJavaDocLine(" *");
            Parameter parm = method.getParameters().get(0);
            sb.setLength(0);
            sb.append(" * @param ");
            sb.append(parm.getName());
            sb.append(' ');
            sb.append(introspectedColumn.getRemarks());
            method.addJavaDocLine(sb.toString());
            method.addJavaDocLine(" */");
        }
    }

    @Override
    public void addComment(XmlElement xmlElement) {
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
    }

}
